package com.elitel.frame.base.component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.elitel.frame.base.service.CodeService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
//import com.elitel.common.redis.RedisDbIndex;
//import com.elitel.common.redis.service.RedisCacheService;
import com.elitel.frame.main.entity.CodeSystem;
import com.elitel.frame.main.entity.CodeType;
import com.elitel.frame.main.entity.vo.CodeTree;
import com.elitel.frame.main.entity.vo.PageResponseVo;

@Component("redisCatchCode")
public class RedisCatchCode {

    @Resource
    private RedisCacheComponent redisCacheComponent;
    //    @Resource
//    private RedisCacheService redisCacheService;
    @Resource
    private CodeService codeServiceImpl;
    @Resource
    private CacheCollections cacheCollections;

    public PageResponseVo<CodeSystem> getRedisCatchCode(String sysId, String pageNo, String pageSize) {
//		int rdi = RedisDbIndex.value;
//		String key = "CODE_"+sysId;
//		String PageResponse = redisCacheService.getObject(key, rdi);
//		PageResponseVo<CodeSystem> pageResponseVo = new PageResponseVo<CodeSystem>();
//		Map<String,Object> map = JSON.parseObject(PageResponse,Map.class);
//		int count = (int) map.get("total");
//		List<CodeSystem> list = JSON.parseArray(JSON.parseObject(PageResponse).getString("rows"), CodeSystem.class);
//		int a = Integer.parseInt(pageNo);
//		int b = Integer.parseInt(pageSize);
//		int lineStart = (a-1)*b;
//		int lineEnd = a*b;
//		List<CodeSystem> codeList = new ArrayList<CodeSystem>();
//		for (int i = lineStart; i <(lineEnd-lineStart<list.size()-lineEnd?lineEnd:list.size()); i++) {
//			codeList.add(list.get(i));
//		}
//		pageResponseVo.setRows(codeList);
//		pageResponseVo.setTotal(count);

        String codeSystem = cacheCollections.getCodeSystem(sysId);
        PageResponseVo<CodeSystem> pageResponseVo = new PageResponseVo<CodeSystem>();
        Map<String, Object> map = JSON.parseObject(codeSystem, Map.class);
        int count = (int) map.get("total");
        List<CodeSystem> list = JSON.parseArray(JSON.parseObject(codeSystem).getString("rows"), CodeSystem.class);
        int a = Integer.parseInt(pageNo);
        int b = Integer.parseInt(pageSize);
        int lineStart = (a - 1) * b;
        int lineEnd = a * b;
        List<CodeSystem> codeList = new ArrayList<CodeSystem>();
        for (int i = lineStart; i < (lineEnd - lineStart < list.size() - lineEnd ? lineEnd : list.size()); i++) {
            codeList.add(list.get(i));
        }
        pageResponseVo.setRows(codeList);
        pageResponseVo.setTotal(count);
        return pageResponseVo;
    }

    @Cacheable(cacheNames = "DEFAULT", key = "'CODE_type'")
    public List<CodeTree> getRedisCatchCodeType() {
//    public List<CodeTree> getRedisCatchCodeType(String key) {
//        int rdi = RedisDbIndex.value;
//        String codeType = redisCacheService.getObject(key, rdi);
//        JSONArray jsonArray = JSONArray.fromObject(codeType);
//        List<CodeTree> list = (List<CodeTree>) jsonArray.toList(jsonArray, CodeTree.class);

        List<CodeTree> list2 = new ArrayList<CodeTree>();
        List<CodeType> list = codeServiceImpl.selectCodeType();
        for (CodeType codeType : list) {
            CodeTree codeTree = new CodeTree();
            codeTree.setId(codeType.getId());
            codeTree.setName(codeType.getTypeName());
            list2.add(codeTree);
        }
        return list2;
    }


    @Cacheable(cacheNames = "DEFAULT", key = "'CODE_'+#id")
    public CodeType selectCodeOne(String id) {
//        int rdi = RedisDbIndex.value;
//        String codeTypeOne = redisCacheService.getObject(key, rdi);
//        JSONObject jsonObject = JSONObject.fromObject(codeTypeOne);
//        CodeType codeType = (CodeType) JSONObject.toBean(jsonObject, CodeType.class);

        CodeType codeType = codeServiceImpl.selectCodeTypeOne(id);
        return codeType;
    }

    @CachePut(value = CacheConfig.DEFAULT, key = "'CODE_'+#sysId")
    public String refreshCodeSystem(String sysId) {
        PageResponseVo<CodeSystem> codeAll = codeServiceImpl.getAllCode(sysId);
        String codeString = JSONObject.fromObject(codeAll).toString();
        return codeString;
    }

    @CachePut(value = CacheConfig.DEFAULT, key = "'CODE_type'")
    public String refreshCodeType() {
        List<CodeType> list = codeServiceImpl.selectCodeType();
        List<CodeTree> list2 = new ArrayList<CodeTree>();
        for (CodeType codeTypes : list) {
            CodeTree codeTree = new CodeTree();
            codeTree.setId(codeTypes.getId());
            codeTree.setName(codeTypes.getTypeName());
            list2.add(codeTree);
        }
        String codeTypeing = JSON.toJSONString(list2);
        return codeTypeing;
    }
}
